<link rel="stylesheet" href="/assets/home/plugins/mescroll/mescroll.min.css">
<link rel="stylesheet" href="/assets/home/css/search.css">

<!-- 搜索框
<div class="mui-input-row mui-search">
    <input type="search" class="mui-input-clear" placeholder="请输入课程名称" name="search" />
</div> -->
<header class="mui-bar mui-bar-nav mui-bar-nav-bg">
    <a id="icon-menu" class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
    <h1 class="mui-title">我的课程</h1>
</header>
<br>
<br>
<br>
<!-- 课程列表 -->
<div id="subject" class="mescroll">
    <div class="list">
    </div>
</div>

<!-- 底部 -->
{include file='common/footer'/}

<script src="/assets/home/plugins/mescroll/mescroll.min.js"></script>

<script>
    // 关键字
    let search = '';

    // 实例化插件
    let meScroll = new MeScroll('subject', {
        // 下拉刷新
        down: {
            auto: false,//是否在初始化完毕之后自动执行下拉回调callback; 默认true
            callback: downCallback // 下拉刷新的回调,别写成downCallback(),多了括号就自动执行方法了
        },
        // 上拉加载
        up: {
            auto: true, //是否在初始化时以上拉加载的方式自动加载第一页数据; 默认false
            isBounce: false, //此处禁止ios回弹,解析(务必认真阅读,特别是最后一点): http://www.mescroll.com/qa.html#q10
            callback: UpCallback, //上拉回调,此处可简写; 相当于 callback: function (page) { upCallback(page); }
            toTop: { //配置回到顶部按钮
                duration: 600,
                src: "/assets/home/images/mescroll-totop.png", //默认滚动到1000px显示,可配置offset修改
                offset: 100
            }
        }
    });


    // 下拉刷新
    async function downCallback() {
        meScroll.resetUpScroll();

        $('#subject .list').empty();

        return false;
    }

    // 上拉加载
    async function UpCallback(page) {
        let data = {
            page: page.num,
            limit: page.size,
            // search
        }

        let result = await POST({
            url: 'business/course',
            params: data
        });

        // console.log(result);
        
        if (result.code === 1) {
            let list = result.data.list ?? [];
            let sublist = result.data.sublist;
            let count = result.data.count;
          
            meScroll.endBySize(list.length, count);

            fetchList(list,sublist);
        }
    }

    function fetchList(list = [],sublist) {
        if (list.length > 0) {
            let Item = '';

            for (let i = 0;i < list.length;i++)  {
                Item = `
                <div class="item">
                    <a href="{:url('/home/index/collect')}?subject=${list[i].sub.id}}">
                        <div class="avatar">
                            <img src="${list[i].sub.thumb_cdn}" alt="">
                        </div>
                        <div class="info">
                            <h5 class="title">${list[i].sub.title}</h5>
                            <p class="type">课程分类： ${sublist[i].category.name}</p>
                            <p class="teacher">授课老师：${sublist[i].teacher.name}</p>
                            <p class="time">发布时间：${list[i].sub.createtime_text}</p>
                            <div class="like">
                                <div class="left">
                                    <img src="/assets/home/images/thumbs-up.png" alt="">
                                    <span>${list[i].sub.likes}</span>
                                </div>
            
                                <span class="price">￥${list[i].sub.price}</span>
                            </div>
                        </div>
                    </a>
                </div>
                `

                $('#subject .list').append(Item);
            }
        }
    }

    $('.mui-search input[type="search"]').keydown(function (e) {
        if (e.keyCode == 13 || e.keyCode == 108) {
            search = $(this).val();

            downCallback();

            return false;
        }
    })
</script>